<style type="text/css">
	 .box {
	 	font-size: 14px
	 }
	 .floatleft {
	 	float: left;
	 	padding: 0;
	 	top: 0px !important;
	 	position: relative;
	 }
	 table {
	 	font-size: 1.1em;
	 	padding: 1em;
	 }
	 input, textarea {
	 	padding: 0px !important;
	 }
</style>
<?php $this->load->library("enc"); ?>
<div class="box">
	<div class="title"><h5>Credit Limit Sales</h5></div>
	<h2><?php echo $territory[0]['territory_name'] ?></h2>

	<strong style="margin:10px">Pilih Nama Sales</strong>
	<select id="sales_name" style="margin:10px;">
		<?php 
			foreach ($sales as $key) {
				if ($key->user_id != $this->enc->defog($this->uri->segment(3))) {
					echo '<option value="'.$key->user_id.'">'.$key->user_name.'</option>';
				}
				else {
					echo '<option value="'.$key->user_id.'" selected="selected">'.$key->user_name.'</option>';
				}
			}
		?>
	</select>
	
</div>

<div>

<table width="100%">
	<tr>
		<?php 
			$limit = explode(';', $sales_detail['credit_limit']['limit_allowed']);
			//20121807 ayu
			if ($limit[0] != 0) {
		?>
		<td width="40%">
			<form>
				<table class='item-list' width="80%">
					<thead>
					<tr>
						<th colspan="2" class="sales-type"><?php echo $sales_detail['credit_limit']['user_group_name'];?></th>
					</tr>
					</thead>
		 			<tr>
						<td width="140px">Nama</td>
						<td class="sales-name"><strong><?=$sales_detail['credit_limit']['user_name']?></strong></td>
					</tr>
					<tr>
						<td>Limit Maksimal</td>
						<td class="credit-limit">Rp. <?php echo number_format($sales_detail['credit_limit']['credit_limit'], '2', '.', ','); ?></td>
					</tr>
					<tr>
						<td>Limit terpakai</td>
						<td class="sales-limit">Rp. <?php echo number_format($sales_detail['credit_limit']['current_limit'], '2', '.', ','); ?></td>
					</tr>
					<tr>
						<td>Limit Baru</td>
						<td><input type="text" name="user_limit" id="new-limit" onkeyup="dolimit()" /></td>
					</tr>
					<tr>
						<td><div class="floatleft">Remark</div></td>
						<td><textarea name="remark" class="remark" cols="50"></textarea></td>
					</tr> 
				</table>

				<!-- <p>&nbsp;&nbsp; &nbsp;&nbsp; <small>1. Limit harus diantara <?=$limit[0]?> dan <?=$limit[1]?> </small></p> -->
				<p>&nbsp;&nbsp; &nbsp;&nbsp; <small>1. Limit harus diantara <?=number_format($limit[0])?> dan <?=number_format($limit[1])?> </small></p> <!-- 20120719 ayu -->
				<p>&nbsp;&nbsp; &nbsp;&nbsp; <small>2. Limit harus lebih besar dari "Limit Terpakai" </small></p>
				<p>&nbsp;</p>
				<!-- <button id="button-back">Kembali</button> -->
				<button id="button-save">Simpan</button>
			</form>
		</td>
		<?php } ?>
		<td width="55%">
			<table class='item-list' width="90%">
				<thead>
					<tr>
						<th width="80px">Tanggal</th>
						<th width="120px">Diketahui Oleh</th>
						<th width="120px">Batas Limit</th>
						<th>Catatan</th>
					</tr>
				</thead>
				<tbody class="history">
					<?php
						if ($sales_detail['history']) {
							foreach ($sales_detail['history'] as $history) {
								echo '<tr>';
								echo '<td>'.$history['limit_date'].'</td>';
								echo '<td>'.$history['user_name'].'</td>';
								echo '<td align="right">Rp. '.number_format($history['limit_value'], '2', '.', ',').'</td>';
								echo '<td>'.$history['remark'].'</td>';
								echo '</tr>';
							}
						}
						else {
							echo "<tr> <td colspan='4' align='center'> Belum pernah update credit limit </td> </tr>";
						}
					?>
				</tbody>
			</table>
			&nbsp;&nbsp; * 10 history credit limit terbaru
		</td>
	</tr>
</table>
</div>


<script type="text/javascript">
	var limit = '<?=$sales_detail["credit_limit"]["limit_allowed"]?>'.split(';');
	var current_limit = <?=$sales_detail["credit_limit"]["current_limit"]?>;

	$(document).ready(function(){
		if(limit[0] == 0) {
			alert('Anda tidak diizinkan untuk menjalankan modul ini \n Silakan koordinasi dengan Cluster Coordinator');
		}
		
		$('#button-save').attr("disabled", "disabled");
		$('#button-save').click(function(event){ event.preventDefault(); submit_form($('#new-limit').val() )} );
		$('form').keydown(function(event){
			if(event.keyCode == 13) {
				event.preventDefault();
				return false;
			}
		});

		$('#sales_name').change(function(){
			update($('#sales_name').val());
		});
	});

	function update(user_id) {
		$.ajax({
			url: "<?=base_url()?>index.php/scan/credit_limit_user/"+$('#sales_name').val()+"/p",
			success: function(data){
				$('.sales-type').html(data["credit_limit"]["user_group_name"]);
				$('.sales-name').html('<strong>'+data["credit_limit"]["user_name"]+'</strong>');
				$('.sales-limit').html('Rp. '+digit_grouping(data["credit_limit"]["current_limit"]) );
				$('.credit-limit').html('Rp. '+digit_grouping(data["credit_limit"]["credit_limit"]) );
				$('.history').empty();
				if (!data['history']) {
					var new_update = "<tr> <td colspan='4' align='center'> Belum pernah update credit limit </td> </tr>";
					$('.history').html(new_update);
				}
				else {
					$.each(data['history'], function(i,n){
						var new_update = "<tr>"+
										"<td>"+n['limit_date']+"</td>"+
										"<td>"+n['user_name']+"</td>"+
										"<td align='right'>Rp."+digit_grouping(n['limit_value'])+"</td>"+
										"<td>"+n['remark']+"</td>"+
										"</tr>";
						$('.history').append(new_update);

					});
				}
				ceklimit(data["credit_limit"]["limit_allowed"]);
			},
			dataType: "json"
		});
	}
	function digit_grouping(value){
		value += '';
		x = value.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '.00';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2 ;
	}
	function ceklimit(limit_value) {
		var limit = limit_value.split(';');
		return limit;
	}
	function dolimit(){
		var new_limit = $('#new-limit').val();
		if (parseInt(new_limit) >= parseInt(limit[0]) && 
			parseInt(new_limit) <= parseInt(limit[1]) && 
			parseInt(new_limit) > parseInt(current_limit) 
		   ) {
			$('#button-save').removeAttr("disabled");
			$('form').keydown(function(event){
				if(event.keyCode == 13) {
					submit_form(new_limit);
				}
			});
		}
		else {
			$('#button-save').attr("disabled", "disabled");
		}
	}

	function submit_form(new_limit) {
		// alert(new_limit);
		var remark = $(".remark").val();

		if (!remark || remark == ' ' || remark == '-') {
			alert ('Remark tidak boleh kosong');
		}
		else {
			$.ajax({
				type: "POST",
				url: '<?=base_url()?>index.php/scan/save_credit_limit/'+$('#sales_name').val(),
				data: {limit:parseInt(new_limit), remark:remark},
				success: function(data){
					alert("Success");
					update(data['user_id']);
					$('input, textarea').val("");//$('textarea').val("");
				}
			});
			$('#button-save').attr("disabled", "disabled");
		}
	}

</script>
